package exception;

public class ThrowableMethods {
    public static void main(String[] args) {
        try {
            int result = 10 / 0;
        } catch (ArithmeticException e) {
            // 1. 获取异常消息
            System.out.println("getMessage(): " + e.getMessage());

            // 2. 获取异常的字符串表示
            System.out.println("toString(): " + e.toString());

            // 3. 打印异常堆栈跟踪（最常用）
            System.out.println("printStackTrace():");
            e.printStackTrace();
        }

        try {
            testExceptionChain();
        } catch (Exception e) {
            System.out.println("\n异常链:");
            e.printStackTrace();
        }
    }

    public static void testExceptionChain() throws Exception {
        try {
            int[] arr = new int[0];
            System.out.println(arr[0]);
        } catch (ArrayIndexOutOfBoundsException e) {
            // 4. 异常链：包装原始异常并抛出新异常
            throw new Exception("处理数组时发生错误", e);
        }
    }
}
